var app = getApp();

   //引用星星的js
var util = require('../../utils/util.js')

Page({
  data: {
    inTheaters:{},
    comingSoon:{},
    top250:{},
    searchResult:{},
    containerShow: true,
    searchPannelShow: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (event) {
      //豆瓣正在热映API
    var inTheatersUrl = app.globalData.doubanBase + '/v2/movie/in_theaters'+"?start=0&count=3";

      //豆瓣即将上印的API
    var comingSoonUrl = app.globalData.doubanBase + '/v2/movie/coming_soon' + "?start=0&count=3";

      //豆瓣TOP250
    var top250Url = app.globalData.doubanBase + '/v2/movie/top250' + "?start=0&count=3";

    this.getMovieListData(inTheatersUrl,"inTheaters","正在热映")
    this.getMovieListData(comingSoonUrl,"comingSoon","即将上映")
    this.getMovieListData(top250Url,"top250","豆瓣排行")

  },
 
     //封装成发送请求url的函数
  getMovieListData: function (url, settedKey, cateTitle) {
      var that = this
      wx.request({
        url: url,
        method: 'GET',
        header: {
          "content-Type": "application/xml"
        },
        success: function (res) {
          that.processDoubanData(res.data, settedKey, cateTitle)
        },
        fail: function (error) {
        }
      })
    },

     //手指点击事件
  onBindFocus:function(e) {
    this.setData({
       containerShow: false,
       searchPannelShow: true
    })
  },

     //关闭按钮
  onCancelImgTap:function(e) {
     this.setData({
       containerShow: true,
       searchPannelShow: false,
      //搜索关闭之后的数据制空
      searchResult: {}

     })
  },
    // 搜索框的改变
  onBindChange:function(e) {
    var text = e.detail.value
    var searchUrl = app.globalData.doubanBase + "/v2/movie/search?q=" + text;
    this.getMovieListData(searchUrl, "searchResult", "")
  },
   


     //豆瓣的数据绑定
  processDoubanData: function (moviesDouban, settedKey, cateTitle) {
      var movies = []
      for(var idx in moviesDouban.subjects) {
        var subject = moviesDouban.subjects[idx];
        var title = subject.title;
         //如果title文字大于6  将截取为 ...
        if(title.length >= 6) {
          title = title.substring(0,6) + "...";
        }
        // 5.0[1,1,1,1,1]  3.0[1,1,1,0,0]
      
        var temp = {
           title: title,
           //综合评分
           average:subject.rating.average,
           //封面海报
           coverageUrl:subject.images.large,
            //ID 为了详情页面跳转
           movieId:subject.id,
           //星星的评分
          stars: util.convertToStarsArray(subject.rating.stars)
            }
        movies.push(temp)
      }
     var readyData = {};
   readyData[settedKey] = {
     cateTitle: cateTitle,
     movies: movies
   }
      this.setData(readyData)
   },
     //跳转到更多电影
  onMoreTap:function(e) {
    var category = e.currentTarget.dataset.category
    wx.navigateTo({
      url: 'more-movie/more-movie?category=' + category,
    })
  },
    //跳转到详情页面
  onMovieTap:function(e) {
    var movieId = e.currentTarget.dataset.movieid
    wx.navigateTo({
      url: "movie-detail/movie-detail?id="+movieId
    })
  }
  
})